#!/bin/bash

function get_absolute_path {
  SHELL_PATH=$1
  
  # resolve links - may be a softlink
  while [ -h "$SHELL_PATH" ]; do
    ls=`ls -ld "$SHELL_PATH"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      SHELL_PATH="$link"
    else
      SHELL_PATH=`dirname "$SHELL_PATH"`/"$link"
    fi
  done
  
  # get directory
  local SHELL_DIR=`dirname "$SHELL_PATH"`
  echo `cd "$SHELL_DIR" ; pwd`
}

WARN_CAPACITY=10
ROOT_PATH=`get_absolute_path $0`
TEMP_FILE=$ROOT_PATH/temp.txt

df -l -B128M > $TEMP_FILE

if [ "`head -n 1 $TEMP_FILE`" = 'Filesystem         128M-blocks      Used Available Use% Mounted on' ]
then
  AVAILABLE_CAPACITY=`awk 'NR > 1 {SUM+=$4} END{print SUM}' $TEMP_FILE`
  TOTAL_CAPACITY=`awk 'NR > 1 {SUM+=$2} END{print SUM}' $TEMP_FILE`
  #RESULT=`echo "scale=2; $TOTAL_CAPACITY/$AVAILABLE_CAPACITY" | bc`
  #RESULT=$((TOTAL_CAPACITY*100/AVAILABLE_CAPACITY))
  RESULT=`echo "$AVAILABLE_CAPACITY*100/$TOTAL_CAPACITY" | bc`
  if [ $RESULT -le ${WARN_CAPACITY} ]
  then
  	echo "Not enough disk space left $RESULT%"
  else
  	echo "Enough disk space left $RESULT%"
  fi
fi

rm -f $TEMP_FILE
